<?php
/**
 * 城池
 * Enter description here ...
 * @author Administrator
 *
 */
class Module_overlordcity{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	function index(){
		return $this->citygold();
	}
	/**
	 * 城池税收初始化
	 * Enter description here ...
	 */
	function citygold(){
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		$key = 'user_'.$uin;
		
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUser($uin);
		if (empty($result)){
			return View::error('君主ID不存在.');
		}
		//挑衅语
		$provoke = App_Overlord_GameStart::provoke();
		
		$city = App_Overlord_Query::oneCity($uin);
		if (empty($city) || empty($city['Value']['hkee'])){
			return View::error('数据异常.');
		}
		//加载物品表
		loadLib('App_Overlord_Collocate');
		//功勋建筑
		$data = App_Overlord_Collocate::getExploitMaterial();
		//取得建筑加成
		$count = 0;
		//获取建筑表
		$exploit_info = App_Overlord_Query::oneExploit_Build($uin);
		if (!empty($exploit_info)){
			foreach ($exploit_info['Value'] as $k => $row){
				//建筑数量为0不计算
				if (!empty($row['count'])){
					$count += $data[$k]['num'] * $row['count'];
				}
			}
		}
		//查询个人武将
		$generals_key = 'generals_'.$uin;
		$generals_result = App_Overlord_Query::oneUin_Generals($uin);
		loadLib('User');
		//任务状态，收复城池，攻打黄巾军
		if ($city['Value']['hkee']['to_uin'] == -1){
			$city['Value']['hkee']['name'] = '毛贼';
			$city['Value']['hkee']['citygold'] = App_Overlord_GameStart::getCityUser($city['Value']['hkee']['lv']);
			$city['Value']['hkee']['end_time'] = App_Overlord_GameStart::getEndDate($city['Value']['hkee']['time']);
		}else{
			foreach ($city['Value'] as  $k => $row){
				if ($k == 'hkee'){
					$city['Value'][$k]['end_time'] = App_Overlord_GameStart::getEndDate($row['time']);
					
					if (!empty($city['Value'][$k]['to_uin']) && $city['Value'][$k]['to_uin'] != $uin){
						$info = App_Overlord_Query::oneUser($city['Value'][$k]['to_uin']);
						if (!empty($info)){
							$city['Value'][$k]['name'] = $info['Value']['name'];
						}
					}			
					$city['Value'][$k]['citygold'] = App_Overlord_GameStart::getCityUser($city['Value'][$k]['lv']);
					if (!empty($city['Value'][$k]['to_uin']) && $city['Value'][$k]['to_uin'] == $uin){
						$city['Value'][$k]['citygold_exploit'] = $city['Value'][$k]['citygold'] * ($count/100);
						//武将驻防收益
						$citygold = App_Overlord_Collocate::getCityGold($generals_result['Value'][$city['Value'][$k]['generals']]['lv']);
						$city['Value'][$k]['citygold_addition'] = (1 + $citygold[$city['Value']['hkee']['lv']] / 100) * $city['Value'][$k]['citygold'];
					}
				}/*else{
					
					foreach ($city['Value']['assist'] as $j => $rows){
						$city['Value']['assist'][$j]['end_time'] =  App_Overlord_GameStart::getEndDate($rows['time']);
						if (!empty($rows['to_uin']) && $rows['to_uin'] != $uin){
							$info = User::getBaseInfo($rows['to_uin']);
							$city['Value']['assist'][$j]['name'] = $info['name'];
						}
						$city['Value']['assist'][$j]['citygold'] = App_Overlord_GameStart::getCityUser($rows['lv']);
					}
				}*/
			}
		}
		
	//已占领城池
		if (!empty($generals_result['Value'])){
			foreach ($generals_result['Value'] as $k => $row){
				if (!empty($row['to_uin']) && $row['to_uin'] != $uin){
					$city_to_uin_array[] =  $row['to_uin'];
					$city_array[] =  $row['to_uin'];
					/*
					$generals_user_info = User::getBaseInfo($row['to_uin']);
					$generals_uin_result[$k] = $row;
					$generals_uin_result[$k]['name'] = $generals_user_info['name'];
					$end_time = $row['time'];
					$generals_uin_result[$k]['end_time'] = App_Overlord_GameStart::getEndDate($end_time);*/
				}elseif ($row['to_uin'] == $uin){
					$city['Value']['hkee']['make'] = array_sum($row['make']) ;
				}
			}
			if (!empty($city_to_uin_array)){
				$generals_user_info = App_Overlord_Query::allUser($city_to_uin_array);
				$city_generals_info = App_Overlord_Query::allCity($city_array);
				if (!empty($generals_user_info)){
					foreach ($generals_user_info as $k => $row){
						$user_array_key = ltrim($row['Key'] , 'user_');
						$generals_user_array[$user_array_key] = $row;
					}
				}
				if (!empty($city_generals_info)){
					foreach ($city_generals_info as $k => $row){
						$city_array_key = ltrim($row['Key'] , 'city_');
						$city_generals_array[$city_array_key] = $row;
					}
				}	
				if (!empty($generals_user_info)){
					foreach ($generals_result['Value'] as $k => $row){
						if (!empty($row['to_uin']) && $row['to_uin'] != $uin){
							$end_time_array[] = $row['time'];
							$generals_uin_result[$k] = $row;
							$generals_uin_result[$k]['to_name'] = $generals_user_array[$row['to_uin']]['Value']['name'];
							$generals_uin_result[$k]['id'] = $k;
							if (!empty($city_generals_array[$k])){
								$generals_uin_result[$k]['lv'] = $city_generals_array[$k]['Value']['hkee']['lv'];
							}
						}
					}
					if (!empty($end_time_array)){
						$city_time_array = App_Overlord_GameStart::getGeneralsTime($end_time_array);
						$end_time = App_Overlord_GameStart::getEndDate($city_time_array[0]);
						$smarty->assign('end_time',$end_time);
					}
				}
			}	
			if(!empty($generals_uin_result)){
	
				/*******************数据分页******************/
				$page = ($page-1) * $per_page;
				$count = count($generals_uin_result);
				$list = array_slice($generals_uin_result,$page,$per_page);
				$smarty->assign('is_list',$list);
				$smarty->assign('count',$count);
				$smarty->assign('per_page',$per_page);
			}
		}

		if (empty($generals_uin_result)){
			loadLib('Friend');
			$friend_list = Friend::get($uin);
			
	//		$friend_list_info = User::getBaseInfo($friend_list);
			//少于3级、取3级的可战信息
			if ($result['Value']['lv'] < 4){
				$result['Value']['lv'] += 1;
			}
			//查出可战人数
			$attack_user_id = App_Overlord_GameStart::getLevelUser($result['Value']['lv'] , 6);
			if (!empty($attack_user_id)){
				//可战的ID
				$city_user = array();
				
				//查询随机攻占城池信息
				$attack_dukes_info = App_Overlord_Query::allCity($attack_user_id);
				//查询好友游戏资料
				$attack_to_user_info = App_Overlord_Query::allUser($attack_user_id);
				if (!empty($attack_to_user_info)){
					foreach ($attack_to_user_info as $k => $row){
						$k_uin = ltrim($row['Key'], 'user_');
						$attack_info[$k_uin] = $row;
					}
				}
			
				if (!empty($attack_dukes_info)){ 
					foreach ($attack_dukes_info as  $kii => $rs){
						$k_uin = ltrim($rs['Key'], 'city_');
						if ($rs['Value']['hkee']['to_uin'] != $uin){		
							$attack_user_info[$k_uin] = $attack_info[$k_uin]['Value'];
							$attack_user_info[$k_uin]['to_uin'] = $rs['Value']['hkee']['to_uin'];
							$attack_user_info[$k_uin]['generals'] = $rs['Value']['hkee']['generals'];
							if (!empty($rs['Value']['hkee']['generals_name'])){
								$attack_user_info[$k_uin]['generals_name'] = $rs['Value']['hkee']['generals_name'];
							}
						}
					}
				}
			}
			if (empty($attack_user_info)){
				$attack_count = 0;
				if (!empty($city_user)){
					//查询好友城池信息
					$attack_dukes_info = App_Overlord_Query::allCity($friend_list);
					//查询好友游戏资料
					$attack_to_user_info = App_Overlord_Query::allUser($friend_list);
				}
				if (!empty($attack_to_user_info)){
					foreach ($attack_to_user_info as $k => $row){
						$k_uin = ltrim($row['Key'], 'user_');
						$attack_info[$k_uin] = $row;
					}
				}
				
				if (!empty($attack_dukes_info)){ 
					foreach ($attack_dukes_info as  $k => $rs){
						$k_uin = ltrim($rs['Key'], 'city_');
						$min_lv = $result['Value']['lv'] - 4;
						$max_lv = $result['Value']['lv'] + 4;
						if ($rs['Value']['hkee']['lv'] > $min_lv && $rs['Value']['hkee']['lv'] < $max_lv && $rs['Value']['hkee']['to_uin'] != $uin){
							$attack_user_info[$k_uin] = $attack_info[$k_uin]['Value'];
							$attack_user_info[$k_uin]['to_uin'] = $rs['Value']['hkee']['to_uin'];
							$attack_user_info[$k_uin]['generals'] = $rs['Value']['hkee']['generals'];
							if (!empty($rs['Value']['hkee']['generals_name'])){
								$attack_user_info[$k_uin]['generals_name'] = $rs['Value']['hkee']['generals_name'];
							}
							$attack_count ++;
							if ($attack_count >= 6) break;
						}
					}
				}
			}
		}
		if (!empty($city['Value']['hkee']['provoke'])){
			$str = $city['Value']['hkee']['provoke'];
			$content = $str;
			if( substr($str , 0 , 3) == '[[#' && substr($str , 4 , 3) == '#]]') {
			    // 替换一下就好
			    $id = substr($str , 3 , 1);
			    if (!empty($provoke[$id])) $content = $provoke[$id];
			}
			
			$smarty->assign('city_provoke',$content);
		}
		//可战城池
		if (!empty($attack_user_info)){
			$smarty->assign('attack_user_info' , $attack_user_info);
		}
		$smarty->assign('provoke' , $provoke);
		$smarty->assign('list',$city['Value']);
		$smarty->assign('uin',$uin);
		return $smarty->fetch('app/Overlord/city.tpl.php');
	}
	/**
	 * 城池税收
	 * Enter description here ...
	 */
	function collectgold(){
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		if (empty($type)){
			return View::error('没有此类型.');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		$key = 'user_'.$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		$result = App_Overlord_Query::oneUser($uin);
		if (empty($result)){
			return View::error('你的君主ID不存在.');
		}
		$city_key = 'city_' .$uin;
		$list = App_Overlord_Query::oneCity($uin);
		
		if ($type == 'hkee'){
			//加载物品表
			loadLib('App_Overlord_Collocate');
			//功勋建筑
			$data = App_Overlord_Collocate::getExploitMaterial();
			//取得建筑加成
			$count = 0;
			//获取建筑表
			$exploit_info = App_Overlord_Query::oneExploit_Build($uin);
			if (!empty($exploit_info)){
				foreach ($exploit_info['Value'] as $k => $row){
					//建筑数量为0不计算
					if (!empty($row['count'])){
						$count += $data[$k]['num'] * $row['count'];
					}
				}
			}
			if (empty($list['Value'][$type])){
				return View::error('你的城池ID错误.');
			}
			if ($list['Value'][$type]['time'] - $time > 0){
				return View::error('你的城池还没能收取税收,请返回刷新试试.');
			}
			$list['Value'][$type]['time'] = $time + 6 * 3600;
			$citygold = App_Overlord_GameStart::getCityUser($list['Value'][$type]['lv']);
		
			//黄金加成0
			$citygold_addition = 0;
			if (!empty($list['Value'][$type]['to_uin']) && $list['Value'][$type]['to_uin'] == $uin){
				//查询个人武将
				$generals_key = 'generals_'.$uin;
				$generals_result = App_Overlord_Query::oneUin_Generals($uin);
				if (empty($generals_result)){
					return View::error('无武将.');
				}
				
				$citygold_addition = $citygold * ($count/100);
				//武将驻防收益
				$city_gold = App_Overlord_Collocate::getCityGold($generals_result['Value'][$list['Value'][$type]['generals']]['lv']);
			
				$citygold_addition += (1 + $city_gold[$list['Value'][$type]['lv']] / 100) * $citygold;
			}
		
			//加成黄金算入
			$citygold += $citygold_addition;
			$result['Value']['gold'] += $citygold;
			
			loadLib('App_Overlord_Task');
			$task_type = App_Overlord_Task::getTaskType();
			App_Overlord_Task::getGo('freshman' , $task_type['freshman']['collect']);
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=city&id='.$citygold);
		}elseif($type == 'gold'){
			$in = isset($_POST['in']) ? trim($_POST['in']) : '';
			$id = isset($_POST['id']) ? trim($_POST['id']) : '';
			if (empty($id)){
				return View::error('数据异常.');
			}
			$generals_key = 'generals_'.$uin;
			$generals_result = App_Overlord_Query::oneUin_Generals($uin);
			
			if (empty($generals_result['Value'])){
				return View::error('数据异常.');
			}
			if (empty($generals_result['Value'][$in]) || $generals_result['Value'][$in]['to_uin'] != $id){
				return View::error('数据异常.');
			}
			
			if ($generals_result['Value'][$in]['time'] - $time > 0){
				return View::error('城池还没能收取税收,请返回刷新试试.');
			}
			$to_uin = $generals_result['Value'][$in]['to_uin'];
			//对方城池
			$to_city = App_Overlord_Query::oneCity($to_uin);
			if (empty($to_city)){
				return View::error('对方城池不存在.');
			}
			//对方城池等级
			$to_city_lv = $to_city['Value']['hkee']['lv'];
			$citygold = App_Overlord_GameStart::getCaptureCity($to_city_lv);
			if (empty($generals_result['Value'][$in]['end_time'])){
				$gold_num = intval(($time - $generals_result['Value'][$in]['time'] ) / (2 * 3600));
				if ($gold_num > 6){
					$gold_num = 5 ;
				}
				$citygold += $citygold * $gold_num;
			}
			$result['Value']['gold'] += $citygold;
			$generals_result['Value'][$in]['time'] = $time + 2 * 3600;
			$code = App_Overlord_Query::alterUin_Generals($uin , $generals_result['Value']);
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=city&id='.$citygold.'&to_uin='.$id);
		}
		$code = App_Overlord_Query::alterUser($uin , $result['Value']);
		//修改城池状态
		App_Overlord_Query::alterCity($uin , $list['Value']);
		
		
	}
	/**
	 * 对方的城池
	 * Enter description here ...
	 */
	function to_city(){
		$smarty = loadSmarty();
		$to_uin = isset($_GET['to_uin']) ? trim($_GET['to_uin']) : '';
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		if (empty($to_uin)){
			return View::error('参数错误.');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//对方的君主信息
		$result = App_Overlord_Query::oneUser($to_uin);
		//对方的城池信息
		$city_info = App_Overlord_Query::oneCity($to_uin);
		if(empty($result)){
			return View::error('没有该玩家ID');
		}
		if(empty($city_info)){
			return View::error('获取城池失败');
		}
		if ($to_uin == $uin) {
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordcity&a=citygold');
			break;
		} 
		//挑衅语
		$provoke = App_Overlord_GameStart::provoke();
		//查询好友
		loadLib('Friend');
		$friend_list = Friend::get($uin);
		$in = in_array($to_uin,$friend_list);
		
		if (!empty($city_info['Value']['hkee']['to_uin']) && $city_info['Value']['hkee']['to_uin'] != -1){
			$is_info = App_Overlord_Query::oneUser($city_info['Value']['hkee']['to_uin']);
			if (empty($is_info)){
				return View::error('数据异常!');
			}
			$smarty->assign('is_info' , $is_info['Value']);
			if ($city_info['Value']['hkee']['to_uin'] == $uin){
				$generals_info = App_Overlord_Query::oneUin_Generals($city_info['Value']['hkee']['to_uin']);
				$id = $city_info['Value']['hkee']['generals'];
				if (empty($generals_info['Value'][$id])){
					return View::error('数据异常!');
				}
				$generals_info['Value'][$id]['end_time'] = App_Overlord_GameStart::getEndDate($generals_info['Value'][$id]['time']);
				$smarty->assign('generals_info',$generals_info['Value'][$id]);
				//查询可收税金
				$gold = App_Overlord_GameStart::getCaptureCity($result['Value']['lv']);
				if (empty($gold)){
					$gold = 0;
				}
				if (empty($generals_info['Value'][$id]['end_time'])){
					$gold_num = intval(($time - $generals_info['Value'][$id]['time'] ) / (2 * 3600));
					if ($gold_num > 6){
						$gold_num = 5 ;
					}
					$gold += $gold * $gold_num;
				}
				$smarty->assign('gold' , $gold);
			}
		}	
		if (!empty($city_info['Value']['hkee']['provoke'])){
			$str = $city_info['Value']['hkee']['provoke'];
			$content = $str;
			if( substr($str , 0 , 3) == '[[#' && substr($str , 4 , 3) == '#]]') {
			    // 替换一下就好
			    $id = substr($str , 3 , 1);
			    if (!empty($provoke[$id])) $content = $provoke[$id];
			}
			
			$smarty->assign('city_provoke',$content);
		}
		$smarty->assign('provoke',$provoke);
		$smarty->assign('list',$result);
		$smarty->assign('city',$city_info);
		$smarty->assign('key',$uin);
		$smarty->assign('is_key',$to_uin);
		$smarty->assign('in',$in);
		return $smarty->fetch('app/Overlord/to_uin.tpl.php');
	}
	/**
	 * 城池一键收取
	 */
	function onekeycity(){
		$doPost = isset($_POST['doPost']) ? intval($_POST['doPost']) : null;
		if ($doPost != 1){
			return View::error('参数错误!');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		$key = 'user_'.$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		$result = App_Overlord_Query::oneUser($uin);
		if (empty($result)){
			return View::error('你的君主ID不存在.');
		}
		//看武将资料
		$list = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($list)){
			return View::error('数据异常.');
		}
		
		$citygold = 0;
		$gold = 0;
		foreach ($list['Value'] as $k => $row){
			//占领的不是自己的城
			if ($row['to_uin'] != $uin && !empty($row['make']) && $row['time'] - $time < 0){
				$list['Value'][$k]['time'] = $time + 2 * 3600;
				//对方城池
				$to_city = App_Overlord_Query::oneCity($row['to_uin']);
				if (!empty($to_city)){
					//对方城池等级
					$to_city_lv = $to_city['Value']['hkee']['lv'];
					$gold = App_Overlord_GameStart::getCaptureCity($to_city_lv);
					$gold_num = intval(($time - $generals_result['Value'][$in]['time'] ) / (2 * 3600));
					$citygold += $gold;
					if ($gold_num > 6){
						$gold_num = 5 ;
					}
					if (!empty($gold_num)){
						$citygold += $gold * $gold_num;
					}
				}
			}
		}	
		if (!empty($citygold)){
			//加黄金
			$result['Value']['gold'] += $citygold;
			//修改武将
			App_Overlord_Query::alterUin_Generals($uin, $list['Value']);
			//修改君主信息
			App_Overlord_Query::alterUser($uin, $result['Value']);
			//跳转成功页面
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=to_city&id='.$citygold);
		}else{
			return View::error('你还不能收取税收');
		}
	} 
	/**
	 * 材料建筑
	 * Enter description here ...
	 */
	function material(){
		$uin = SessionInfo('uin');
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询是否有材料建筑
		$result = App_Overlord_Query::oneCollocate('app_overlord_build', 'build_' . $uin);
		
		//物品配置类
		loadLib('App_Overlord_Collocate');
		//材料建筑配置
		$data = App_Overlord_Collocate::getMaterial();
		
		if (empty($result)){
			//初始化材料建筑
			$value = array('lv' => 1, 'time' => $time);
			App_Overlord_Query::addCollocate('app_overlord_build', 'build_' . $uin, $value);
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordcity&a=material');
			break;
		}
		if (!empty($result['Value']['time'])){
			$result['Value']['end_time'] = App_Overlord_GameStart::getEndDate($result['Value']['time']);
		}
		$smarty = loadSmarty();
		$smarty->assign('data',$data);
		$smarty->assign('list',$result['Value']);
		return $smarty->fetch('app/Overlord/material.tpl.php');
	}
	/**
	 * 介绍
	 * Enter description here ...
	 */
	function materialintroduce(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		//物品配置类
		loadLib('App_Overlord_Collocate');
		//材料建筑配置
		$data = App_Overlord_Collocate::getMaterial();
		if (empty($data[$id])){
			return View::error('没有此类型介绍.');
		}
		$smarty = loadSmarty();
		$smarty->assign('list',$data[$id]);
		return $smarty->fetch('app/Overlord/material_introduce.tpl.php');
	}
	/**
	 * 收取材料
	 */
	function collect(){
		$doPost = isset($_POST['doPost']) ? intval($_POST['doPost']) : 0;
		if(empty($doPost)) {
			return View::error('参数错误.');
		}

		$uin = SessionInfo('uin');
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询是否有材料建筑
		$result = App_Overlord_Query::oneCollocate('app_overlord_build', 'build_' . $uin);
		if (empty($result)){
			return View::error('数据异常.');
		}
		//物品配置类
		loadLib('App_Overlord_Collocate');
		//材料建筑配置
		$data = App_Overlord_Collocate::getMaterial();
		if (!empty($result['Value']['time'])){
			//时间没到、、跳转
			if ($result['Value']['time'] - $time > 0){
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordcity&a=material');
				break;
			}else{
				//循环得出数量
				foreach ($data as $k => $row){
					//6是山寨
					if ($k == 5){
						$barracks = rand(20, 80);
					}else{
						if ($k == 0){
							$rand[$k] = rand(0, 2);
						}else{
							$rand[$k] = rand(0, 6);
						}
						//0是没收到
						if ($rand[$k] == 0){
							$rand[$k] = rand(40, 100);
						}
					}
				}
			//获取任务类型
			loadLib('App_Overlord_Task');
			$task_type = App_Overlord_Task::getTaskType();
			//完成探索宝山任务
			App_Overlord_Task::getGo('main' , $task_type['main']['explore']);
				//查询是否有该人物
				$user_info = App_Overlord_Query::oneUser($uin);
				if (empty($user_info)){
					return View::error('获取君主资料失败.');
				}
				//查询是否有商品
				$items_info = App_Overlord_Query::oneItems($uin);
				
				//查询看是否存在兵营，为收取做准备
				$barracks_info = App_Overlord_Query::oneSoldiers($uin);
				//黄金
				$gold = 0;
				$information = array();
				//物品
				if (!empty($items_info['Value'])){
					//材料
					if (!empty($rand)){
						foreach ($rand as $k => $row){
							//大于40设置为黄金
							if ($row >= 40){
								$gold += $row;
							}else{//宝玉
								if (empty($items_info['Value']['material'][$k]['count'])){
									$items_info['Value']['material'][$k]['count'] = $row;
								}else{
									$items_info['Value']['material'][$k]['count'] += $row; 
								}
								//获取动态消息,材料记录
		//						$information['material'][$k] = $row;
							}
						}
					}
					//物品材料
					$code = App_Overlord_Query::alterItems($uin, $items_info['Value']);
				}
				if (!empty($barracks_info['Value'])){
					//兵类
					if (!empty($barracks)){
						//随机一个数、看是那种兵
						$barracks_rand = rand(0, 3);
						$barracks_info['Value'][$barracks_rand]['count'] += $barracks;	
						//获取动态消息,兵种记录
			//			$information['barracks'] = $barracks;
						//兵营
						$code = App_Overlord_Query::alterSoldiers($uin, $barracks_info['Value']);
					}
				}
				if (!empty($user_info['Value']['gold'])){
					//黄金存在
					if (!empty($gold)){
						$user_info['Value']['gold'] += $gold;
						//君主黄金
						$code = App_Overlord_Query::alterUser($uin, $user_info['Value']);}
				}
				//修改时间，两个小时刷一次
				$result['Value']['time'] = $time + 2 * 3600;
				$code = App_Overlord_Query::alterCollocate('app_overlord_build', 'build_' . $uin, $result['Value']);
				if ($code == 0){
					App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=collect&id='.$barracks.'&type_id='.$barracks_rand.'&lan='.$rand[0].'&tao='.$rand[1].'&jing='.$rand[2].'&xi='.$rand[3].'&hu='.$rand[4]);
				}else{
					return View::error('系统繁忙,稍后再试');
				}
			}
		}
	}
	/**
	 * 可占领的城池
	 */
	function generals_city(){
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		loadLib('User');
		//查询君主资料
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主等级失败!');
		}
		$count = 0;
		//查询个人武将
		$generals_result = App_Overlord_Query::oneUin_Generals($uin);
		if (!empty($generals_result['Value'])){
			foreach ($generals_result['Value'] as $k => $row){
				if (!empty($row['to_uin']) && $row['to_uin'] != $uin){
					$user_array[] = $row['to_uin'];
	//				$generals_user_info = User::getBaseInfo($row['to_uin']);
	//				$generals_uin_result[$k] = $row;
	//				$generals_uin_result[$k]['name'] = $generals_user_info['name'];
			//		$count += 1;
				}
			}	
			if (!empty($user_array)){
				$generals_user_info = App_Overlord_Query::allUser($user_array);
				if (!empty($generals_user_info)){
					foreach ($generals_user_info as $k => $row){
						$generals_to_uin_result[$row['Key']] = $row;
					}
					foreach ($generals_result['Value'] as $k => $row){
						if (!empty($row['to_uin']) && $row['to_uin'] != $uin){
							$generals_uin_result[$k] = $row;
							$generals_uin_result[$k]['name'] = $generals_to_uin_result['user_' . $row['to_uin']]['Value']['name'];
							$count += 1;
						}
					}
				}
			}	
			if(!empty($generals_uin_result)){
				$smarty->assign('list',$generals_uin_result);
			}
		}
	
		//可攻占城池数量
		$city_count = App_Overlord_GameStart::getCityCount($user_info['Value']['lv']);
		$smarty->assign('city_count',$city_count);
		$smarty->assign('count',$count);
		return $smarty->fetch('app/Overlord/generals_city.tpl.php');
	}
	/**
	 * 可攻占城池
	 */
	function battlecity(){
		$uin = SessionInfo('uin');
		loadLib('App_Overlord_GameStart');
		loadLib('User');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUser($uin);
		if (empty($result)){
			return View::error('君主ID不存在.');
		}
		if (empty($result['Value']['task'])){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordguide&a=guide');
			break;
		}
		$user_lv = $result['Value']['lv'];
		//少于3级、取3级的可战信息
		if ($result['Value']['lv'] < 4){
			$user_lv += 1;
		}
	//查出可战人数
		$attack_user_id = App_Overlord_GameStart::getLevelUser($user_lv , 8);
		if (!empty($attack_user_id)){
		$city_user = array();
			
		//查询随机攻占城池信息
		$attack_dukes_info = App_Overlord_Query::allCity($attack_user_id);
		//查询好友游戏资料
		$attack_to_user_info = App_Overlord_Query::allUser($attack_user_id);
		if (!empty($attack_to_user_info)){
			foreach ($attack_to_user_info as $k => $row){
				$k_uin = ltrim($row['Key'], 'user_');
				$attack_info[$k_uin] = $row;
			}
		}
	
		if (!empty($attack_dukes_info)){ 
			foreach ($attack_dukes_info as  $kii => $rs){
				$k_uin = ltrim($rs['Key'], 'city_');
				if ($rs['Value']['hkee']['to_uin'] != $uin){		
					$attack_user_info[$k_uin] = $attack_info[$k_uin]['Value'];
					$attack_user_info[$k_uin]['to_uin'] = $rs['Value']['hkee']['to_uin'];
					$attack_user_info[$k_uin]['generals'] = $rs['Value']['hkee']['generals'];
					if (!empty($rs['Value']['hkee']['generals_name'])){
						$attack_user_info[$k_uin]['generals_name'] = $rs['Value']['hkee']['generals_name'];
					}
				}
			}
		}
		}
		$smarty = loadSmarty();
		if (!empty($attack_user_info)){
			$smarty->assign('attack_user_info',$attack_user_info);
		}
		$smarty->assign('key',$uin);
		return $smarty->fetch('app/Overlord/battlecity.tpl.php');
	}
}
# end of script
